/**
 * Created by dev-04 on 2016/8/29.
 */

var assert = require('assert');
var mongodb = require("mongodb");

var mongoClient = mongodb.MongoClient;
var url = "mongodb://localhost:27017/testproject";
mongoClient.connect(url, function (err, db) {
    assert.equal(null, err);
    console.log("Connected correctly to server");

    //
    // Case 1: List all databases of admin
    //
    //var adminDb = db.admin();
    //adminDb.listDatabases(function(err, dbs) {
    //    assert.equal(null, err);
    //    assert.ok(dbs.databases.length > 0);
    //    for (tdb in dbs.databases)
    //    {
    //        console.log(tdb.name);
    //    }
    //    db.close();
    //});

    //
    // Case 2: Create Capped Collection
    //
    //db.createCollection("myCappedCollection", {"capped": true, "size": 100000, "max": 5000 },
    //    function (err, results){
    //        console.log("Collection Created!!");
    //        db.close();
    //    }
    //);

    //
    // Case 3: Create Collection with validation
    //
    //db.createCollection("validatedCollection", {
    //    'validator': { '$or':
    //        [
    //            { 'phone': { '$type': "string" } },
    //            { 'email': { '$regex': /@mongodb\.com$/ } },
    //            { 'status': { '$in': [ "Unknown", "Incomplete" ] } }
    //        ]
    //    }
    //}, function(err, results){
    //    console.log("Collection With validation created.");
    //    db.close();
    //});

    // Case 4: Create indexes ...

    // Case 5: CRUD Operations

    // insert document
    //var insert_table = "inserts";
    //db.collection(insert_table).insertOne({a: 1}, function(err, results) {
    //    assert(1, results); // the results here actually is the rows effected
    //    assert(true, err);
    //    console.log("Insert one record successfully!");
    //    db.collection(insert_table).insertMany([{a:2}, {a:3}], function(err, results) {
    //        assert(true, err);
    //        assert(2, results);
    //
    //        console.log("Insert Many record successfully!");
    //
    //        db.close();
    //    });
    //});

    // update document
    //var update_table = "updates";
    //db.collection(update_table).insertMany([{a:1}, {a:2}, {a:2}, {a:3}], function(err, results){
    //    assert.equal(null, err);
    //    assert.equal(4, results.insertedCount);
    //    console.log("Insert Many record successfully!");
    //
    //    db.collection(update_table).updateOne({a:1}, {$set: {b : 1}}, function(err, results){
    //        assert.equal(null, err);
    //        assert.equal(1, results.matchedCount);
    //        assert.equal(1, results.modifiedCount);
    //        console.log("Update one record successfully!");
    //
    //        db.collection(update_table).updateMany({a:2}, {$set: {c: 1}}, function(err, results){
    //            assert.equal(null, err);
    //            assert.equal(2, results.matchedCount);
    //            assert.equal(2, results.modifiedCount);
    //
    //            console.log("Update many record successfully!");
    //            // Upsert a single document
    //            db.collection(update_table).updateOne({a:3}, {$set: {d: 1}}, {
    //                upsert: true
    //            }, function(err, r) {
    //                assert.equal(null, err);
    //                assert.equal(1, r.matchedCount);
    //                assert.equal(0, r.upsertedCount);
    //
    //                console.log("upsert one record successfully!");
    //                db.close();
    //            });
    //        });
    //
    //    });
    //});

    // Delete document
    var delete_table = "deletes";
    db.collection(delete_table).d
});